package com.coder520.service.impl;

import com.coder520.dao.UserMapper;
import com.coder520.pojo.User;
import com.coder520.service.IUserService;
import com.coder520.util.MD5Utils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * Created by ASUS on 2017/7/4.
 */
@Service("iUserService")
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;


    public User checkUser(String username,String password) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        if(username==null || password==null){
            return null;
        }
        password = MD5Utils.encryptPassword(password);
        User user = userMapper.selectByUsernameAndPassword(username,password);
        return user;
    }

    @Transactional
    public void regist(User user) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        user.setPassword(MD5Utils.encryptPassword(user.getPassword()));
        userMapper.insert(user);
    }

    public List<User> findUserAll(Long roleid){
        List<User> userList;
        if(roleid==null){
            userList= userMapper.selectUserAll(null);
        }else {
            userList= userMapper.selectUserAll(roleid);

        }
        if(CollectionUtils.isNotEmpty(userList)){
            return userList;
        }
        return null;
    }

}
